package com.creditease.volatileDemo;

public class VolatileDemo {

	private volatile int number=0;
	
	public void increase() throws InterruptedException{//���ܱ�֤ԭ����
		Thread.sleep(100);
		//number++�Ĳ�����Ϊ������裺
		//1.���ڴ��ȡnumber��ֵ
		//2.��numberֵ��1
		//3.��numberֻд�����ڴ�
		//������Ϊ���ϲ�����volatile�޷���֤ԭ����
		this.number++;
	}
	public static void main(String[] args) {
		final VolatileDemo volDemo = new VolatileDemo();
		
		for(int i=0;i<500;i++){
			new Thread(
					new Runnable(){
						public void run() {
							try {
								volDemo.increase();
							} catch (InterruptedException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
						
					}
					).start();
		}
		//��������߳���ִ�У����߳̾��ó�CPU��Դ
		//֪���������̶߳�������ɣ����߳��ڼ�������ִ��
		while(Thread.activeCount()>1){
			Thread.yield();
		}

		System.out.println(volDemo.number);
	}

}
